<?php
defined('IN_XLP') or exit('Access Denied');

class returnModel extends Model {

	function __construct() {
		parent::__construct();
		$this->dbTable = 'return_log';
		$this->page = 20;
	}

	/**
	 * 客服处理退款
	 * @param  int $order_id 订单ID
	 * @param  int $agree	 1同意 0不同意
	 * @param  int $admin_id 管理员ID
	 * @param  string $note  处理原因备注
	 */
	public function service_refund($order_id, $agree, $admin_id, $note) {
		if($agree) {
			$order_refund_state = 2;  //同意退款
			$log_refund_state   = 2;
		} else {
			$order_refund_state = 4;  //拒绝退款
			$log_refund_state   = 3;
		}

		//修改订单状态
		if($agree) {
			M('order')->update(
                array('refund_state'=>$order_refund_state, 'order_state'=>62), 
                array('id'=>$order_id)
            );	
		} else {
			//拒绝则订单关闭
			M('order')->update(
				array(
					'refund_state'=>$order_refund_state,
					'finish_time'=>time(),
				),
				array('id'=>$order_id)
			);
		}
		
		//修改退款退货日志状态
		M('return_log')->update(
			array(
				'service_id'   => $admin_id,
				'service_msg'  => $note,
				'service_time' => time(),
				'refund_state' => $log_refund_state
			),
			array('order_id'=>$order_id)
		);

		return true;
	}

	/**
	 * 客服处理退货
	 * @param  int $order_id 订单ID
	 * @param  int $agree	1同意 0不同意
	 * @param  int $admin_id 管理员ID
	 * @param  string $note  处理原因备注
	 */
	public function service_return($order_id, $agree, $admin_id, $note) {
		if($agree) {
			$order_return_state = 2;  //同意退货
			$log_refund_state   = 2;
		} else {
			$order_return_state = 4;  //拒绝退货
			$log_refund_state   = 3;
		}
		//修改订单状态
		if($agree) {
			M('order')->update(
                array('return_state'=>$order_return_state, 'order_state'=>64), 
                array('id'=>$order_id)
            );	
		} else {
			//拒绝则订单关闭
			M('order')->update(
				array('return_state'=>$order_return_state, 'finish_time'=>time()),
				array('id'=>$order_id)
			);
		}
		
		//修改退款退货日志状态
		M('return_log')->update(
			array(
				'service_id'   => $admin_id,
				'service_msg'  => $note,
				'service_time' => time(),
				'refund_state' => $log_refund_state
			),
			array('order_id'=>$order_id)
		);

		return true;
	}

	/**
	 * 财务处理退款
	 * @param  int $order_id		订单ID
	 * @param  int $admin_id		管理员ID
	 * @param  array $actual_refund 实际退款金额 array('order_goods表ID'=>'退款金额')
	 * @param  string $note		    退款备注
	 * @return boolean			    处理状态
	 */
	public function finance_refund($order_id, $admin_id, $actual_refund, $note) {
		//修改订单商品表
		$total_refund = 0;
		foreach ($actual_refund as $k => $v) {
			M('order_goods')->update(array('actual_refund'=>$v), array('id'=>$k));
			$total_refund += $v;
		}

		//修改订单状态
		M('order')->update(
			array(
				'finish_time'   => time(),
				'refund_state'  => 3,  //已退款
				'actual_refund' => $total_refund,
				'order_state'   => 70,  //退款已确认
			), 
			array('id'=>$order_id)
		);

		//修改退款退货日志状态
		M('return_log')->update(
			array(
				'finance_id'   => $admin_id,
				'finance_msg'  => $note,
				'finance_time' => time(),
				'refund_state' => 4,	//已经退款
			),
			array('order_id'=>$order_id)
		);

		//添加订单日志
		$result = D('order')->log($order_id, 70, $note, 'admin-'.$admin_id);

		return $result;
	}

	/**
	 * 财务处理退货退款
	 * @param  int $order_id		订单ID
	 * @param  int $admin_id		管理员ID
	 * @param  array $actual_refund 实际退款金额 array('order_goods表ID'=>array('refund'=>'退款金额','return'=>'退货数量'))
	 * @param  string $note		    退款备注
	 * @return boolean			    处理状态
	 */
	public function finance_return($order_id, $admin_id, $actual_refund, $note) {
		//修改订单商品表
        $total_refund = 0;
        $total_return = 0;
        foreach ($actual_refund as $k => $v) {
            M('order_goods')->update(
            	array('actual_refund'=>$v['refund'], 'actual_return'=>$v['return']),
            	array('id'=>$k)
            );
            $total_refund += $v['refund'];
            $total_return += $v['return'];
        }

        //修改订单状态
        M('order')->update(
            array(
                'finish_time'   => time(),
                'refund_state'  => 3,  //已退货退款
                'actual_refund' => $total_refund,
                'actual_return' => $total_return,
                'order_state'   => 71,  //退货退款已确认
            ),
            array('id'=>$order_id)
        );

        //修改退款退货日志状态
        M('return_log')->update(
            array(
                'finance_id'   => $admin_id,
                'finance_msg'  => $note,
                'finance_time' => time(),
                'refund_state' => 4,    //已经退货退款
            ),
            array('order_id'=>$order_id)
        );

        //添加订单日志
        $result = D('order')->log($order_id, 71, $note, 'admin-'.$admin_id);

        return $result;
	}
}